//全选伪数组
var checkAllEles = document.querySelectorAll(".check-all");
//单选伪数组
var checkOneEles = document.querySelectorAll(".check-one");
//单价伪数组
var priceEles = document.querySelectorAll(".price");
//总价伪数组
var subEles = document.querySelectorAll(".subtotal");
//对象数组
var objArr = [] ;
//添加按钮伪数组
var addEles = document.querySelectorAll(".add");
//减少伪数组
var reduceEles = document.querySelectorAll(".reduce");
//数量
var countEles = document.querySelectorAll(".count-input");
//总价
var priceTotal = document.getElementById("priceTotal");
//选择的总量
var selectedTotal = document.getElementById("selectedTotal");
//全删
var deleteAll = document.getElementById("deleteAll");
//单删伪数组
var delBtns = document.querySelectorAll(".delete");
//所有tr标签伪数组
var trEles = document.querySelectorAll("tbody tr");

/**
* 全选联动单选
*/
checkAllEles.forEach(function(checkAllEle,each,arr){
    checkAllEle.onclick = function(){
        if (checkAllEle.checked) {
            checkOneEles.forEach(function(checkOneEle,key){
                var obj = {} ;
                obj.flag = true ; 
                obj.price = priceEles[key].innerHTML  ;
                obj.count = parseInt(countEles[key].value) ;
                checkOneEle.checked = checkAllEle.checked ;
                if ( objArr[key] == undefined ){
                    objArr.push(obj);
                } else {
                    objArr.splice(key,1,obj);
                }
            });
            priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
            selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
        } else {
            checkOneEles.forEach(function(checkOneEle,key){
                checkOneEle.checked = checkAllEle.checked ;
                var obj = {} ;
                obj.flag = false ; 
                obj.price = priceEles[key].innerHTML  ;
                obj.count = parseInt(countEles[key].value) ;
                if ( objArr[key] == undefined ){
                    objArr.push(obj);
                } else {
                    objArr.splice(key,1,obj);
                }
            });
            priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
            selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
        }
        arr.forEach(function(item){
            item.checked = checkAllEle.checked ;
        });
    }
});

/**
* 单选联动全选
*/
checkOneEles.forEach(function( checkOneEle , key , arr ){
    checkOneEle.onclick = function (){
        var obj = {} ;
        obj.flag = false ; 
        obj.price = priceEles[key].innerHTML  ;
        obj.count = parseInt(countEles[key].value) ;
        if ( objArr[key] == undefined ){
            objArr.push(obj);
        } else {
            objArr.splice(key,1,obj);
        }
        if(checkOneEle.checked){
            obj.flag = checkOneEle.checked ; 
            var res = Array.from(arr).every(function( item ){
                return item.checked
            });
            if (res){
                checkAllEles[0].click()
            } 
        } else {
            checkAllEles.forEach(function( checkAllEle ){
                checkAllEle.checked = false ;
            });  
        }
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
    }
});
/**
* 商品数量的增加与减少
*/
addEles.forEach(function( addEle , key ){
    addEle.onclick = function (){
        var obj = {} ;
        obj.flag = checkOneEles[key].checked ; 
        obj.price = priceEles[key].innerHTML  ;
        
        countEles[key].value = parseInt(countEles[key].value) + 1;
        obj.count = countEles[key].value ; 

        if ( objArr[key] == undefined ){
            objArr.push(obj);
        } else {
            objArr.splice(key,1,obj);
        }

        counts = countEles[key].value ;
        if( counts > 1 ){
            reduceEles[key].innerHTML ="-" ; 
        } 
        subEles[key].innerHTML = `${(countPrice(obj.price,obj.count)).toFixed(2)}`
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
    }
});

reduceEles.forEach(function( reduceEle , key ){
    if( countEles[key].value == 1 ){
        reduceEles[key].innerHTML ="" ; 
    } 
    reduceEle.onclick = function(){
        if( parseInt(countEles[key].value) > 1 ){
            countEles[key].value = parseInt(countEles[key].value) - 1; 
            if( parseInt(countEles[key].value) == 1 ){
                reduceEle.innerHTML ="" ; 
            }
        }
        var obj = {} ;
        obj.flag = checkOneEles[key].checked ; 
        obj.price = priceEles[key].innerHTML ;
        obj.count = countEles[key].value ; 
        if ( objArr[key] == undefined ){
            objArr.push(obj);
        } else {
            objArr.splice(key,1,obj);
        }
        subEles[key].innerHTML = `${(countPrice(obj.price,obj.count)).toFixed(2)}`
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ; 
    }
});

/**
 * 单删
 */
 delBtns.forEach(function(delBtn,key){
    delBtn.onclick = function (){
        // console.log(priceEles[key].innerHTML);
        var length = objArr.length ; 
        // console.log(objArr[i].price == priceEles[key].innerHTML);
        for( var i = 0 ; i < length ; i ++ ){
            // console.log(objArr);
            // console.log(objArr[i].price)
            // console.log(objArr[i].price == priceEles[key].innerHTML);
            for( var j in objArr[i] ){
                if(objArr[i][j] == priceEles[key].innerHTML){
                    objArr.splice(key,1);
                }
            }
            // if(objArr[i].price == priceEles[key].innerHTML){
            //     objArr.splice(i,1);
            // }
        }
        trEles[key].remove();
        priceTotal.innerHTML = `${(totalPrice(objArr)).toFixed(2)}`;
        selectedTotal.innerHTML = `${totalCount(objArr)}` ;
        console.log(objArr);
    }
 });


/**
 * 单个商品的价格
 */
function countPrice(price,count) {
    return price * count ; 
}

/**
 * 所有的商品价格和数量以及是否勾选确定最后总价
 */
function totalPrice( arr ){
   var total = 0 ;
   arr.forEach(function ( obj , key ){
       if (obj.flag) {
           total += countPrice(obj.price,obj.count); 
       }
   });
   return total ; 
}

/**
 * 商品数量
 */
function totalCount( arr ){
    var total = 0 ; 
    arr.forEach(function ( obj ){
       if (obj.flag) {
           total += parseInt(obj.count) ;
       }
   });
   return total ;
}